<template>
  <div class="exam-page">
    <el-card>
      <template #header>
        <div class="exam-header">
          <h2>{{ examInfo.name }}</h2>
          <div class="exam-timer">
            <el-tag type="warning" size="large">
              剩余时间: {{ formatTime(remainingTime) }}
            </el-tag>
          </div>
        </div>
      </template>

      <div class="question-list">
        <div v-for="(question, index) in questions" :key="question.id" class="question-item">
          <div class="question-header">
            <div class="question-title">
              <span class="question-id">第 {{ index + 1 }} 题</span>
              <span class="question-type-tag">{{ getQuestionType(question.type) }}</span>
              <span class="question-score">{{ question.score }}分</span>
            </div>
          </div>
          
          <div class="question-body">
            <div class="content-text">{{ question.content }}</div>
          </div>

          <div class="question-options" v-if="question.type === 'single' || question.type === 'multiple'">
            <div v-for="option in question.options" :key="option.key" class="option-item">
              <el-radio 
                v-if="question.type === 'single'" 
                v-model="answers[question.id]" 
                :label="option.key"
                class="option-selector"
              >
                <span class="option-label">{{ option.key }}.</span>
                <span class="option-content">{{ option.content }}</span>
              </el-radio>
              
              <el-checkbox 
                v-else 
                v-model="answers[question.id]" 
                :label="option.key"
                class="option-selector"
              >
                <span class="option-label">{{ option.key }}.</span>
                <span class="option-content">{{ option.content }}</span>
              </el-checkbox>
            </div>
          </div>

          <el-input
            v-else-if="question.type === 'fill'"
            v-model="answers[question.id]"
            type="textarea"
            :rows="3"
            placeholder="请输入答案"
            class="answer-input"
          />

          <el-input
            v-else-if="question.type === 'essay'"
            v-model="answers[question.id]"
            type="textarea"
            :rows="6"
            placeholder="请输入答案"
            class="answer-input"
          />
        </div>
      </div>

      <div class="exam-footer">
        <el-button type="primary" @click="submitExam" :loading="submitting">
          提交试卷
        </el-button>
      </div>
    </el-card>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted, onUnmounted } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { ElMessage, ElMessageBox } from 'element-plus'

interface Question {
  id: string
  type: 'single' | 'multiple' | 'fill' | 'essay'
  content: string
  options?: Array<{
    key: string
    content: string
  }>
  score: number
}

interface ExamInfo {
  id: string
  name: string
  duration: number
}

const route = useRoute()
const router = useRouter()
const examInfo = ref<ExamInfo>({
  id: '',
  name: '',
  duration: 120
})
const questions = ref<Question[]>([])
const answers = ref<Record<string, any>>({})
const remainingTime = ref(0)
const timer = ref<number | null>(null)
const submitting = ref(false)

const fetchExamInfo = async () => {
  try {
    examInfo.value = {
      id: route.params.id as string,
      name: '示例考试',
      duration: 120
    }
    remainingTime.value = examInfo.value.duration * 60
  } catch (error) {
    ElMessage.error('获取考试信息失败')
    router.push('/student/exam-management')
  }
}

const fetchQuestions = async () => {
  try {
    questions.value = [
      {
        id: '1',
        type: 'single',
        content: '这是一道单选题',
        options: [
          { key: 'A', content: '选项A' },
          { key: 'B', content: '选项B' },
          { key: 'C', content: '选项C' },
          { key: 'D', content: '选项D' }
        ],
        score: 5
      },
      {
        id: '2',
        type: 'multiple',
        content: '这是一道多选题',
        options: [
          { key: 'A', content: '选项A' },
          { key: 'B', content: '选项B' },
          { key: 'C', content: '选项C' },
          { key: 'D', content: '选项D' }
        ],
        score: 10
      },
      {
        id: '3',
        type: 'fill',
        content: '这是一道填空题',
        score: 5
      },
      {
        id: '4',
        type: 'essay',
        content: '这是一道简答题',
        score: 20
      }
    ]
  } catch (error) {
    ElMessage.error('获取题目失败')
  }
}

const formatTime = (seconds: number) => {
  const hours = Math.floor(seconds / 3600)
  const minutes = Math.floor((seconds % 3600) / 60)
  const secs = seconds % 60
  return `${hours.toString().padStart(2, '0')}:${minutes.toString().padStart(2, '0')}:${secs.toString().padStart(2, '0')}`
}

const getQuestionType = (type: string) => {
  const types: Record<string, string> = {
    single: '单选题',
    multiple: '多选题',
    fill: '填空题',
    essay: '简答题'
  }
  return types[type] || '未知类型'
}

const startTimer = () => {
  timer.value = window.setInterval(() => {
    if (remainingTime.value > 0) {
      remainingTime.value--
    } else {
      submitExam()
    }
  }, 1000)
}

const submitExam = async () => {
  if (submitting.value) return
  
  try {
    await ElMessageBox.confirm('确定要提交试卷吗？提交后将无法修改答案。', '提示', {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning'
    })
    
    submitting.value = true
    await new Promise(resolve => setTimeout(resolve, 1000))
    ElMessage.success('提交成功')
    router.push('/student/exam-management')
  } catch (error) {
    if (error !== 'cancel') {
      ElMessage.error('提交失败')
    }
  } finally {
    submitting.value = false
  }
}

onMounted(async () => {
  await fetchExamInfo()
  await fetchQuestions()
  startTimer()
})

onUnmounted(() => {
  if (timer.value) {
    clearInterval(timer.value)
  }
})
</script>

<style scoped>
.exam-page {
  padding: 20px;
  background-color: #f5f7fa;
  min-height: 100vh;
}

.exam-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}

.exam-header h2 {
  margin: 0;
  font-size: 18px;
  color: #303133;
  font-weight: 600;
}

.question-list {
  margin-top: 20px;
}

.question-item {
  padding: 24px;
  background-color: white;
  border-radius: 8px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.05);
  margin-bottom: 20px;
}

.question-item:last-child {
  margin-bottom: 0;
}

.question-header {
  margin-bottom: 24px;
  padding-bottom: 16px;
  border-bottom: 1px solid #ebeef5;
}

.question-title {
  display: flex;
  align-items: center;
  gap: 12px;
}

.question-id {
  font-size: 20px;
  font-weight: 600;
  color: #303133;
}

.question-type-tag {
  font-size: 14px;
  color: #409eff;
  background-color: #ecf5ff;
  padding: 4px 12px;
  border-radius: 4px;
}

.question-score {
  font-size: 14px;
  color: #f56c6c;
  background-color: #fef0f0;
  padding: 4px 12px;
  border-radius: 4px;
}

.question-body {
  margin-bottom: 24px;
  line-height: 1.6;
}

.content-text {
  font-size: 16px;
  color: #303133;
  line-height: 1.8;
}

.question-options {
  margin: 24px 0;
  padding: 16px;
  background-color: #f5f7fa;
  border-radius: 8px;
}

.option-item {
  margin-bottom: 16px;
}

.option-item:last-child {
  margin-bottom: 0;
}

.option-selector {
  width: 100%;
  padding: 12px;
  border-radius: 4px;
  transition: all 0.3s;
}

.option-selector:hover {
  background-color: #ecf5ff;
}

.option-label {
  font-weight: 600;
  color: #409eff;
  margin-right: 8px;
}

.option-content {
  color: #303133;
}

.answer-input {
  margin: 24px 0;
}

.answer-input :deep(.el-textarea__inner) {
  padding: 12px;
  font-size: 14px;
  line-height: 1.6;
  border-color: #dcdfe6;
  border-radius: 4px;
}

.answer-input :deep(.el-textarea__inner:focus) {
  border-color: #409eff;
}

.exam-footer {
  margin-top: 32px;
  text-align: center;
  padding-top: 16px;
  border-top: 1px solid #ebeef5;
}

:deep(.el-radio),
:deep(.el-checkbox) {
  margin-right: 0;
  height: auto;
  line-height: 1.6;
}

:deep(.el-radio__label),
:deep(.el-checkbox__label) {
  color: #303133;
  font-size: 14px;
}

:deep(.el-button--primary) {
  padding: 12px 30px;
  font-size: 16px;
  font-weight: 500;
}

:deep(.el-card) {
  border-radius: 8px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
}

:deep(.el-card__header) {
  padding: 15px 20px;
  border-bottom: 1px solid #ebeef5;
  background-color: #f5f7fa;
}

:deep(.el-card__body) {
  padding: 20px;
}
</style> 